package cn.fintecher.pangolin.service.manage.web;

import cn.fintecher.pangolin.common.model.CaseFollowRecordMatchModel;
import cn.fintecher.pangolin.common.web.BaseController;
import cn.fintecher.pangolin.entity.mysql.ContactConfig;
import cn.fintecher.pangolin.service.manage.request.AddContactConfigRequest;
import cn.fintecher.pangolin.service.manage.response.ContactConfigResponse;
import cn.fintecher.pangolin.service.manage.response.ContactConfigTreeResponse;
import cn.fintecher.pangolin.service.manage.response.PrincipalResponse;
import cn.fintecher.pangolin.service.manage.service.ContactConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Set;

/**
 * @Auther: xiaqun
 * @Description: 联络配置相关接口
 * @Date: 10:57 2019/9/12
 */

@RestController
@RequestMapping("/api/contactConfigController")
@Api(value = "联络配置相关接口", description = "联络配置相关接口")
public class ContactConfigController extends BaseController {
    @Autowired
    private ContactConfigService contactConfigService;

    /**
     * 查询联络配置树结构
     */
    @ApiOperation(value = "查询联络配置树结构", notes = "查询联络配置树结构")
    @GetMapping("/getContactConfigTree")
    public ResponseEntity<List<ContactConfigTreeResponse>> getContactConfigTree(@RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to get contact config tree");
        List<ContactConfigTreeResponse> list = contactConfigService.getContactConfigTree(token);
        return ResponseEntity.ok().body(list);
    }

    /**
     * 获取子集
     */
    @ApiOperation(value = "获取子集", notes = "获取子集")
    @GetMapping("/getChildren")
    public ResponseEntity<List<ContactConfig>> getChildren(@RequestParam @ApiParam(value = "父级ID", required = true) String parent) {
        log.info("REST request to get children,{}", parent);
        List<ContactConfig> list = contactConfigService.getChildren(parent);
        return ResponseEntity.ok().body(list);
    }

    /**
     * 根据ID获取配置信息
     */
    @ApiOperation(value = "根据ID获取配置信息", notes = "根据ID获取配置信息")
    @GetMapping("/getContactConfigById")
    public ResponseEntity<ContactConfigResponse> getContactConfigById(@RequestParam @ApiParam(value = "主键ID", required = true) String id) {
        log.info("REST request to get contact config by id,{}", id);
        ContactConfigResponse contactConfig = contactConfigService.getContactConfigById(id);
        return ResponseEntity.ok().body(contactConfig);
    }

    /**
     * 新增联络配置
     */
    @ApiOperation(value = "新增联络配置", notes = "新增联络配置")
    @PostMapping("/addContactConfig")
    public ResponseEntity<Void> addContactConfig(@Valid @RequestBody AddContactConfigRequest request) {
        log.info("REST request to add contact config,{}", request);
        contactConfigService.addContactConfig(request);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改联络配置
     */
    @ApiOperation(value = "修改联络配置", notes = "修改联络配置")
    @PutMapping(value = "/modifyContactConfig")
    public ResponseEntity<Void> modifyContactConfig(@Valid @RequestBody AddContactConfigRequest request) {
        log.info("REST request to modify contact config,{}", request);
        contactConfigService.modifyContactConfig(request);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除联络配置
     */
    @ApiOperation(value = "删除联络配置", notes = "删除联络配置")
    @DeleteMapping("/deleteContactConfig")
    public ResponseEntity<Void> deleteContactConfig(@RequestParam @ApiParam(value = "配置ID", required = true) String id) {
        log.info("REST request to delete contact config,{}", id);
        contactConfigService.deleteContactConfig(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询没有联络配置的委托方
     */
    @ApiOperation(value = "查询没有联络配置的委托方", notes = "查询没有联络配置的委托方")
    @GetMapping("/getPrincipalNoConfig")
    public ResponseEntity<List<PrincipalResponse>> getPrincipalNoConfig(@RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to get principal no config");
        List<PrincipalResponse> list = contactConfigService.getPrincipalNoConfig(token);
        return ResponseEntity.ok().body(list);
    }

    /**
     * 查询跟进记录字段项
     */
    @ApiOperation(value = "查询跟进记录字段项", notes = "查询跟进记录字段项")
    @GetMapping("/getFollowMarchRecord")
    public ResponseEntity<List<CaseFollowRecordMatchModel>> getFollowMarchRecord() {
        log.info("REST request to get follow march record");
        List<CaseFollowRecordMatchModel> list = contactConfigService.getFollowMarchRecord();
        return ResponseEntity.ok().body(list);
    }

    /**
     * 查询跟进记录映射值
     */
    @ApiOperation(value = "查询跟进记录映射值", notes = "查询跟进记录映射值")
    @GetMapping("/getAllContactConfig")
    public ResponseEntity<List<ContactConfig>> getAllContactConfig(@RequestParam @ApiParam(value = "委托方ID", required = true) String principalId) {
        log.info("REST request to get all contact config,{}", principalId);
        List<ContactConfig> list = contactConfigService.getAllContactConfig(principalId);
        return ResponseEntity.ok().body(list);
    }

    /**
     * 查询催记匹配字段
     */
    @ApiOperation(value = "查询催记匹配字段", notes = "查询催记匹配字段")
    @GetMapping("/getFollowRecordFields")
    public ResponseEntity<List<ContactConfigResponse>> getFollowRecordFields(@RequestParam @ApiParam(value = "委托方ID", required = true) String principalId) {
        log.info("REST request to get follow record fields,{}", principalId);
        List<ContactConfigResponse> list = contactConfigService.getFollowRecordFields(principalId);
        return ResponseEntity.ok().body(list);
    }

    /**
     * 查询联系人关系
     */
    @ApiOperation(value = "查询联系人关系", notes = "查询联系人关系")
    @GetMapping("/getRelationShip")
    public ResponseEntity<Set<String>> getRelationShip(@RequestParam @ApiParam(value = "委托方ID", required = true) String principalId) {
        log.info("REST request to get relationship,{}", principalId);
        Set<String> set = contactConfigService.getReflect(principalId, "relation");
        return ResponseEntity.ok().body(set);
    }

    /**
     * 查询地址状态
     */
    @ApiOperation(value = "查询地址状态", notes = "查询地址状态")
    @GetMapping("/getAddressStatus")
    public ResponseEntity<Set<String>> getAddressStatus(@RequestParam @ApiParam(value = "委托方ID", required = true) String principalId) {
        log.info("REST request get address status,{}", principalId);
        Set<String> set = contactConfigService.getReflect(principalId, "addrStatus");
        return ResponseEntity.ok().body(set);
    }

    /**
     * 查询地址类型
     */
    @ApiOperation(value = "查询地址类型", notes = "查询地址类型")
    @GetMapping("/getAddressType")
    public ResponseEntity<Set<String>> getAddressType(@RequestParam @ApiParam(value = "委托方ID", required = true) String principalId) {
        log.info("REST request to get address type,{}", principalId);
        Set<String> set = contactConfigService.getReflect(principalId, "addrType");
        return ResponseEntity.ok().body(set);
    }

    /**
     * 查询联络结果
     */
    @ApiOperation(value = "查询联络结果", notes = "查询联络结果")
    @GetMapping("/getContactResult")
    public ResponseEntity<Set<String>> getContactResult(@RequestParam @ApiParam(value = "委托方ID", required = true) String principalId) {
        log.info("REST request to get contact result,{}", principalId);
        Set<String> set = contactConfigService.getReflect(principalId, "contactResult");
        return ResponseEntity.ok().body(set);
    }
}